package addon.w3.w3x;

import java.io.EOFException;
import java.io.IOException;

import addon.w3.mdx.LEDataInputStream;


/**
 * relation with W3XEnvironment
 * @author madjk
 *
 */
public class W3XTileData extends W3XCom {
	public short height;
	public short waterlevel;
	
	public byte flags[];
	
	public W3XTileData(LEDataInputStream lein) {
		this.lein = lein;
	}
	
//	public W3XTileData(byte data[]) {
//		super(data);
//	}
	
	@Override
	public void _decode() throws W3XException, IOException {
		/*
		 * 
			Data:
			Each tilepoint is defined by a block of 7 bytes.
			The number of blocks is equal to Mx*My.
			short: ground height
			C000h: minimum height (-16384)
			2000h: normal height (ground level 0)
			3FFFh: maximum height (+16383)
			short: water level + map edge boundary flag*(see notes)
			4bit: flags*(see notes)
			4bit: ground texture type (grass, dirt, rocks,...)
			1byte: texture details (of the tile of which the tilepoint is the bottom left corner) (rocks, holes, bones,...). It appears that only a part of this byte is used for details. It needs more investigations
			4bit: cliff texture type
			4bit: layer height
			
			*flags notes:
			Flags values (shown as big endian):
			   0x4000 --> boundary flag 1 (shadow generated by the world editor on the edge of the map)
			   0x0010 --> ramp flag (used to set a ramp between two layers)
			   0x0020 --> blight flag (ground will look like Undead's ground)
			   0x0040 --> water flag (enable water)
			   0x0080 --> boundary flag 2 (used on "camera bounds" area. Usually set by the World Editor "boundary" tool.)
			
			Water level:
			Water level is stored just like ground height. The highest bit (bit 15) is used for the boundary flag 1.
			
			Tilepoint data example:
			51 21 00 62 56 84 13
			51 21 --(little endian)--> 0x2151 --(hex-->dec)--> height = 8529
			00 62 --(little endian)--> 0x6200
			   (extract boundary flag)--> (0x6200 & 0xC000) = 0x4000 boundary flag set
			   (extract water data)--> (0x6200 & 0x3FFF) = 0x2200 --(hex-->dec)--> water level = 8704
			56 --> 5 sets both water flag and ramp flag, 6 means tilepoint is using ground type #6 in the tilesets table
			84 --> means tilepoint is using detail texture #132 (=0x084)
			13 --> 1 means cliff type #1 (cliff tilesets table), 3 means tilepoint on layer "3"
			The tilepoint "final height" you see on the WE is given by:
			(ground_height - 0x2000 + (layer - 2)*0x0200)/4
			where "0x2000" is the "ground zero" level, 2 the "layer zero" level and "0x0200" the layer height
			   = (0x2151 - 0x2000 + 1*0x0200)/4
			   = (8529 - 8192 + 512)/4
			   = 212,25
			
			The tilepoint "water level" you see on the WE is given by:
			(water_level - 0x2000)/4 - 89.6
			where "0x2000" is the "ground zero" level, -89.6 is the water zero level (given by the water.slk file height = -0,7 --> water_zero_level = -0,7*128):
			   = 8704/4 - 89,6
			   = 38,4
			
			In this case, water flag is set and water level is below the ground level so we will not see the water. This is just an example and I don't think you can find such a tilepoint on a map. It was just here for demonstration purpose.
			

		 */
		height = lein.readShort();
		waterlevel = lein.readShort();
		flags = new byte[3];
		lein.read(flags);
		
//		int temp_height = lein.readShort();
//		int temp_water_level = lein.readShort();
//		
//		flags = new byte[3];
//		lein.read(flags);
//		
//		int final_height = ( ( temp_height-0x2000 ) + ( (0x07&flags[2]) - 2 )*0x0200 )/4;
//		height = (short)final_height;
//		
//		int final_water_level = ( temp_water_level-0x2000 )/4 - (int)89.6;
//		waterlevel = (short)final_water_level;
//		
//		System.out.println(lein.toHex(flags, 2, 1));
	}
}